#include <stdio.h>
#include <stdlib.h>

/*
	用选择排序实现降序排列。
*/

int *min(int array[], int left, int right)
{
	int i;
	int *result = &array[left];
	for (i = left; i <= right; i++)
	{
		if (array[i] < *result)
		{
			result = &array[i];
		}
	}
	return result;
}

void swap(int *a, int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

void SelectionSort(int array[], int n, int mode)
{
	int i;
	if (mode < 0)
	{
		for (i = n - 1; i > 0; i--)
		{
			swap(min(array, 0, i), &array[i]);
		}
	}
	else
	{
		for (i = 0; i < n - 1; i++)
		{
			swap(min(array, i, n - 1), &array[i]);
		}
	}
}

int main()
{
	int array[1000];
	int i;
	for (i = 0; i < 1000; i++)
	{
		array[i] = rand() % 10000;
	}
	SelectionSort(array, 1000, -1);
	int tag = 0;
	for (int i = 0; i < 999; i++)
	{
		if (array[i] < array[i + 1])
		{
			tag = -1;
			break;
		}
	}
	printf("tag=%d\n", tag);
	return 0;
}
